package cn.tedu.csmall.product.service.impl;

import cn.tedu.csmall.product.ex.ServiceException;
import cn.tedu.csmall.product.mapper.BrandCategoryMapper;
import cn.tedu.csmall.product.mapper.BrandMapper;
import cn.tedu.csmall.product.mapper.CategoryMapper;
import cn.tedu.csmall.product.pojo.entity.BrandCategory;
import cn.tedu.csmall.product.pojo.vo.BrandCategoryStandardVO;
import cn.tedu.csmall.product.pojo.vo.BrandStandardVO;
import cn.tedu.csmall.product.pojo.vo.CategoryStandardVO;
import cn.tedu.csmall.product.service.IBrandCategoryService;
import cn.tedu.csmall.product.web.ServiceCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Date;

/**
 * @author 何惠民
 */
@Slf4j
@Service
public class BrandCategoryServiceImpl implements IBrandCategoryService {


    @Autowired
    BrandCategoryMapper brandCategoryMapper;

    @Autowired
    BrandMapper brandMapper;

    @Autowired
    CategoryMapper categoryMapper;

    public BrandCategoryServiceImpl() {
        log.debug("创建业务层对象:BrandCategoryServiceImpl");
    }

    @Override
    public void bind(Long brandId, Long categoryId) {
        log.debug("开始处理【绑定品牌与类别的关联】的业务，品牌：{}，类别：{}", brandId, categoryId);

        //查询品牌数据是否存在
         BrandStandardVO brand =  brandMapper.getStandardById(brandId);
         if (brand == null){
             String message = "ID为" + brandId +"品牌数据不存在";
             log.debug(message);
             throw new ServiceException(ServiceCode.ERR_NOT_FOUND,message);
         }

        //查询类别数据是否存在
        CategoryStandardVO category =  categoryMapper.getStandardById(categoryId);
        if (category == null){
            String message = "ID为" + brandId +"类别数据不存在";
            log.debug(message);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND,message);
        }

        //查询查询绑定的关系是否已经存在
        int count =  brandCategoryMapper.countByCategoryIdAndBrandId(brandId,categoryId);
        if (count > 0){
            String message = "品牌id:" + brandId +"分类ID:" + categoryId +"的数据已经存在";
            log.debug(message);
            throw new ServiceException(ServiceCode.ERR_CONFLICT,message);
        }

        BrandCategory brandCategory = new BrandCategory();
        brandCategory.setBrandId(brandId);
        brandCategory.setCategoryId(categoryId);
        brandCategory.setGmtCreate(LocalDateTime.now());
        int rows = brandCategoryMapper.insert(brandCategory);
        if (rows != 1){
            String message = "添加数据失败,服务器忙,请稍后再尝试!";
            log.debug(message);
            throw new ServiceException(ServiceCode.ERR_DELETE,message);
        }
    }

    @Override
    public void unbind(Long brandId,Long categoryId) {
        log.debug("开始处理【解绑品牌与类别的关联】的业务，品牌：{}，类别：{}", brandId, categoryId);

        //查询查询绑定的关系是否已经存在
        int count =  brandCategoryMapper.countByCategoryIdAndBrandId(brandId,categoryId);
        if (count < 1){
            String message = "品牌id:" + brandId +"分类ID:" + categoryId +"的数据不存在";
            log.debug(message);
            throw new ServiceException(ServiceCode.ERR_CONFLICT,message);
        }

        int rows = brandCategoryMapper.deleteByBrandIdAndCategoryId(brandId,categoryId);
        if (rows != 1){
            String message = "删除数据失败,服务器忙,请稍后再尝试!";
            log.debug(message);
            throw new ServiceException(ServiceCode.ERR_DELETE,message);
        }
    }
}
